package com.lmaos.basetools.menuList;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.lmaos.basetools.sText.TextUtils;
/** 这是一个目录
 * 
 * @author lmaos
 *
 * <p>作者：张星宇</p>
 * <p>时间：2017年5月23日 下午11:08:09</p>
 * <p>说明:		</p>
 *
 */
public class MenuDirectory {
	public MenuDirectory(MenuItem dirMenuItem,Map<String,MenuDirectory> directorySearch,Map<String,MenuItem> menuItemSearch) {
		if(dirMenuItem.isMenuList()){
			this.dirMenuItem = dirMenuItem;
			dirPath = dirMenuItem.getAbsoluteMenuPath();
			this.directorySearch = directorySearch;
			this.menuItemSearch = menuItemSearch;
			if(directorySearch!=null)
				directorySearch.put(this.dirPath, this);
			if(menuItemSearch!=null)
				menuItemSearch.put(this.dirPath, this.dirMenuItem);
		}else{
			throw new MenuItemNotListException(dirMenuItem.getAbsoluteMenuPath());
		}
	}
	
	public MenuDirectory(String dirPath,Map<String,MenuDirectory> directorySearch,Map<String,MenuItem> menuItemSearch) {
		this.dirPath = TextUtils.formatTextEnd(dirPath.replace("\\", "/"), "/");
		this.directorySearch = directorySearch;
		this.menuItemSearch = menuItemSearch;
		this.dirMenuItem = new MenuItem(this.dirPath).setTemp(true);
		if(directorySearch!=null)
			directorySearch.put(this.dirPath, this);
		if(menuItemSearch!=null)
			menuItemSearch.put(this.dirPath, this.dirMenuItem);
	}


	private String dirPath = null; 
	private MenuItem dirMenuItem = null; // 目录对应的项
	private Map<String,MenuItem> menuItems = new HashMap<String,MenuItem>();
	private Map<String,MenuDirectory> directories = new HashMap<String,MenuDirectory>();
	private Map<String,MenuDirectory> directorySearch = null;
	private Map<String,MenuItem> menuItemSearch = null;
	/**
	 * 说明：获得当前目录的项数据		<br>
	 * 时间：2017年5月23日 下午11:08:41
	 *
	 * @return
	 */
	public MenuItem getDirMenuItem() {
		return dirMenuItem;
	}
	/**
	 * 说明：获得当前路径下全部目录		<br>
	 * 时间：2017年5月23日 下午11:08:58
	 *
	 * @return
	 */
	public Map<String, MenuDirectory> getDirectories() {
		return new HashMap<String, MenuDirectory>(directories);
	}
	/**
	 * 说明：获取当前路径下全部子项		<br>
	 * 时间：2017年5月23日 下午11:09:24
	 *
	 * @return
	 */
	public Map<String, MenuItem> getMenuItems() {
		Map<String, MenuItem> map = new HashMap<String, MenuItem>(menuItems);
		for (Entry<String, MenuDirectory> dirEntry : directories.entrySet()) {
			MenuDirectory dir = dirEntry.getValue();
			MenuItem menuItem = dir.dirMenuItem;
			map.put(dirEntry.getKey(), menuItem);
		}
		return map;
	}
	/**
	 * 说明：添加项,如果目录不存在则创建目录		<br>
	 * 时间：2017年5月23日 下午11:11:39
	 *
	 * @param menuItem
	 */
	public void addMenuItem(MenuItem menuItem){
		String absolutPath = menuItem.getAbsoluteMenuPath();
		if(dirPath.equals(absolutPath)){
			this.dirMenuItem = menuItem;
			return;
		}
		int dirPath_index = absolutPath.indexOf(dirPath); // 父路径位置
		if(dirPath_index!=-1){
			int nextDirPath_startIndex = dirPath_index+dirPath.length();
			int nextDirPath_endIndex = absolutPath.indexOf("/",nextDirPath_startIndex);
			if(nextDirPath_endIndex == -1){ // 是一个最终文件项
				String path_name =  absolutPath.substring(nextDirPath_startIndex);
				if(!directories.containsKey(path_name)){
					menuItems.put(path_name, menuItem);
				}else{
					throw new MenuItemException("目录不可以直接修改为文件 path:"+absolutPath);
				}
			}else{// 仍然是一个目录
				String path_name =  absolutPath.substring(nextDirPath_startIndex, nextDirPath_endIndex);
				if(menuItems.containsKey(path_name)){
					throw new MenuItemException("文件不可以直接修改为目录 path:"+absolutPath+" name="+path_name);
				}
				
				MenuDirectory directory = directories.get(path_name);
				if(directory==null){
					String dir_path_n = dirPath+path_name;
					directory = new MenuDirectory(dir_path_n,directorySearch,menuItemSearch);
					directories.put(path_name, directory);
				}
				directory.addMenuItem(menuItem);
//				if(nextDirPath_endIndex!=absolutPath.length()-1){
//					directory.addMenuItem(menuItem);
//				}else if(menuItem.isMenuList()){
//					directory.dirMenuItem = menuItem;
//				}
			}
		}
	}
	/**
	 * 说明：移除当前目录中的当前项,通过项名称移除		<br>
	 * 时间：2017年5月23日 下午11:11:58
	 *
	 * @param menuItemName
	 */
	public void removeMenuItemName(String menuItemName){
		menuItems.remove(menuItemName);
	}
	/**
	 * 说明：获得当前目录下的子项		<br>
	 * 时间：2017年5月23日 下午11:12:35
	 *
	 * @param menuItemName
	 * @return
	 */
	public MenuItem getMenuItem(String menuItemName){
		if(TextUtils.isEmpty(menuItemName)){
			return null;
		}
		
		int path_endIndex = menuItemName.lastIndexOf("/");
		if(path_endIndex == -1){ // 直接获得项
			MenuItem menuItem = menuItems.get(menuItemName);
			if(menuItem == null){
				MenuDirectory dir = directories.get(menuItemName);
				if(dir!=null){
					menuItem = dir.dirMenuItem;
				}
			}
			return menuItem;
		}
		
		String dirName = menuItemName.substring(0,path_endIndex);
		MenuDirectory dir = getMenuDirectory(dirName);
		if(dir==null){
			return null;
		}
		if(path_endIndex == menuItemName.length()-1){
			return dir.dirMenuItem;
		}else{
			return dir.menuItems.get(menuItemName.substring(path_endIndex+1));
		}
	}
	public MenuDirectory getMenuDirectory(String dirName){
		String paths[] = dirName.split("/");
		MenuDirectory dir = null;
		Map<String, MenuDirectory> dirs = directories;
		for (int i = 0; i < paths.length; i++) {
			String path = paths[i];
			if(!TextUtils.isEmpty(path)){
				dir = dirs.get(paths[i]);
				if(dir==null){
					return null;
				}
				dirs = dir.directories;
			}
		}
		
		return dir;
	}
	public String getDirPath() {
		return dirPath;
	}
	/** 清空目录
	 * 
	 */
	public void clearDir(){
		menuItems.clear();
		directories.clear();
		dirMenuItem = null;
	}
	
	@Override
	public String toString() {
		return dirPath+"="+directories+"";
	}
	
	public static void main(String[] args) {
		MenuDirectory d = new MenuDirectory("/",null,null);
//		MenuItem m = new MenuItem("/a/b/"); 
		MenuItem m2 = new MenuItem("/a/b/c");//  错误 已经创建文件，不可以作为目录项
//		d.addMenuItem(m);
		d.addMenuItem(m2);
		System.out.println(d.getMenuDirectory("/a/").getMenuItem("b"));
		
	}
}
